function genTextCode(cache) {
  let code = "";
  switch (cache.subType) {
    case "Text":
      code = textCode(cache);
      break;
    case "TextField":
      code = textFieldCode(cache);
      break;
    default:
      code = textCode(cache);
      break;
  }
  return code;
}

function textCode(cache) {
  let data = cache.data;
  const { font, width, height, id, opacity } = data;
  const { content } = font;
  if (font.styles.length > 1) {
    return richTextCode(cache);
  }

  console.log("textCode id: " + id);
  let fontSize = `${textUnit(num(font.size))}`;
  let fontWeight = "fontWeightRegular";
  let color = rgbaToHex(font.styles[0].color.value, opacity / 100);

  if (font.font.includes("Medium") || font.font.includes("Semibold")) {
    fontWeight = "fontWeightMedium";
  } else if (font.font.includes("Bold")) {
    fontWeight = "fontWeightBold";
  }

  let text = genTextArb(content);
  if (text.arb) {
    stringList.push(text.arb);
  }
  console.log(`// ${content}`);

  let code = `
    Text(${text.ref},
      style: TextStyle(
        fontSize: ${fontSize},
        fontWeight: ${fontWeight},
        color: ${color},
  `;

  if (font.font.includes("Italic")) {
    code += `
    fontStyle: FontStyle.italic,
    `;
  }

  if (font.styles[0].underline == "1") {
    code += `
    decoration: TextDecoration.underline,
    decorationColor: ${color},
    `;
  }

  code += `
      ),
  )`;

  return code;
}

function richTextCode(cache) {
  let data = cache.data;
  let code = `
  Text.rich(
    TextSpan(
      children: [
  `;

  for (let i = 0; i < data.font.styles.length; i++) {
    let style = data.font.styles[i];
    let color = rgbaToHex(style.color.value);

    let text = genTextArb(style.content);
    if (text.arb) {
      stringList.push(text.arb);
    }
    console.log(`// ${style.content}`);

    code += `
      TextSpan(
        text: ${text.ref},
        style: TextStyle(
          color: ${color},
    `;
    let fontWeight = "fontWeightRegular";
    if (style.font.includes("Medium") || style.font.includes("Semibold")) {
      fontWeight = "fontWeightMedium";
    } else if (style.font.includes("Bold")) {
      fontWeight = "fontWeightBold";
    }

    code += `
          fontWeight: ${fontWeight},
    `;

    if (style.underline == "1") {
      code += `
          decoration: TextDecoration.underline,
          decorationColor: ${color},
      `;
    }
    if (style.italic == "1") {
      code += `
          fontStyle: FontStyle.italic,
      `;
    }
    code += `
        ),
      ),
    `;
  }

  code += `
      ],
    ),
  )`;

  return code;
}

function textFieldCode(cache) {
  let data = cache.data;
  const { font, width, height, id } = data;
  const { content } = font;
  console.log("textFieldCode id: " + id);
  let fontSize = `${textUnit(font.size)}`;
  let fontWeight = "fontWeightRegular";
  let color = rgbaToHex(font.styles[0].color.value);

  let code = `
  TextField(
    style: TextStyle(
      fontSize: ${fontSize},
      fontWeight: ${fontWeight},
      color: ${color},
    ),
    decoration: InputDecoration(
    hintStyle: TextStyle(
      fontSize: ${fontSize},
      fontWeight: ${fontWeight},
      color: ${color},
    ),
    ),
  )
  `;
  return code;
}
